home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
L' Effet Pommier 3
/
L'Effet Pommier - Volume 03.iso
/
Programmation
/
gray image 2.1
/
image_rect.cc
< prev
next >
Wrap
Text File
|
1995-05-03
|
4KB
|
144 lines
// This may look like C code, but it is really -*- C++ -*-
/*
************************************************************************
*
* Grayscale Image
* Implementation of the Primitive Operations
* on the rectangular area of the image
*
* The image is represented as a Pixmap, i.e. a matrix of pixels
* each of them specifies the gray level at a particular point
*
************************************************************************
*/
#include "image.h"
/*
*------------------------------------------------------------------------
* Construct a new image from the rectangular area of another image
*/
IMAGE::IMAGE(const Rectangle& ra)
{
allocate(ra.nrows,ra.ncols,ra.image.bits_per_pixel);
register GRAY * ps = ra.ptr;
register GRAY * pi = pixels;
register int i,j;
for(i=0; i < ra.nrows; i++, ps += ra.inc_to_nextrow)
for(j=0; j < ra.ncols; j++) // Proceed scanline by scanline
*pi++ = *ps++;
assert( pi == pixels + npixels );
}
/*
*------------------------------------------------------------------------
* Modify all the pixels in the rectangular area
* according to a particular operation
*/
// Perform "pixel OP val"
#define PIXEL_MODIFICATION_OP(OP) \
\
Rectangle& Rectangle::operator OP (const int val) \
{ \
register GRAY * pp = ptr; \
register int i,j; \
\
for(i=0; i < nrows; i++, pp += inc_to_nextrow) \
for(j=0; j < ncols; j++) /*Proceed scanline by scanline*/\
*pp++ OP val; \
\
return *this; \
} \
PIXEL_MODIFICATION_OP(=)
PIXEL_MODIFICATION_OP(+=)
PIXEL_MODIFICATION_OP(-=)
PIXEL_MODIFICATION_OP(*=)
PIXEL_MODIFICATION_OP(|=)
PIXEL_MODIFICATION_OP(&=)
PIXEL_MODIFICATION_OP(^=)
#undef PIXEL_MODIFICATION_OP
// Shift the pixel values
// over the rectangular area to the left
Rectangle& Rectangle::operator <<= (const int val)
{
register GRAY_SIGNED * pp = (GRAY_SIGNED *)ptr;
register int i,j;
if( abs(val) >= GRAY_MAXBIT )
_error("Very fishy shift factor: %d",val);
for(i=0; i < nrows; i++, pp += inc_to_nextrow)
for(j=0; j < ncols; j++) // Proceed scanline by scanline
*pp++ <<= val;
return *this;
}
// Shift the pixel values
// over the rectangular area to the right
Rectangle& Rectangle::operator >>= (const int val)
{
register GRAY_SIGNED * pp = (GRAY_SIGNED *)ptr;
register int i,j;
if( abs(val) >= GRAY_MAXBIT )
_error("Very fishy shift factor: %d",val);
for(i=0; i < nrows; i++, pp += inc_to_nextrow)
for(j=0; j < ncols; j++) // Proceed scanline by scanline
*pp++ >>= val;
return *this;
}
// Get a total sum of all the pixels
// over the rectangular area
double sum_over(const Rectangle& ra)
{
register GRAY_SIGNED * pp = (GRAY_SIGNED *)ra.ptr;
register double sum = 0;
register int i,j;
for(i=0; i < ra.nrows; i++, pp += ra.inc_to_nextrow)
for(j=0; j < ra.ncols; j++) // Proceed scanline by scanline
sum += *pp++;
return sum;
}
/*
*------------------------------------------------------------------------
* Operations on the rectangle area as a whole
*/
// Copy a rectangle a_rect into the
// rectangular area of the image
Rectangle& Rectangle::operator = (const Rectangle& a_rect)
{
if( a_rect.nrows != nrows || a_rect.ncols != ncols )
_error("Rectangles %dx%d and %dx%d are incompatible",
a_rect.nrows,a_rect.ncols,nrows,ncols);
register GRAY * sp = a_rect.ptr; // Source and
register GRAY * dp = ptr; // destination ptrs
register int i;
for(i=0; i < nrows; i++)
memcpy(dp,sp,ncols*sizeof(GRAY)),
sp += a_rect.inc_to_nextrow + ncols,
dp += inc_to_nextrow + ncols;
return *this;
}